from ctypes import CDLL, c_int, POINTER
import ctypes
import numpy as np
# 加载动态链接库
libexample = CDLL('./libexample.so')

# Define the argument types
libexample.go4.argtypes = [ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_int)]
# libexample.go4.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]


array = np.array([
    [3.812703443979303497e+00, -2.201761147783173556e+00, -7.960991276501979463e+00, 1.999999981210338592e+04, -7.052799879974786836e-03, -4.115024631932816951e-02, 3.419286121868728330e-03],
    [3.822507376925853517e+00, 4.159865386520875674e+00, -7.912455602632991969e+00, 1.999999969793388300e+04, 2.860284674968527688e-02, -1.805760783722435911e-02, 3.419286121868728330e-03],
    [3.804955982955721794e+00, 1.886193979317273062e+00, -7.878159532748052030e+00, 1.999999976621795577e+04, -5.996270634108172163e-04, -6.390287932552221606e-02, 3.419286121868728330e-03],
    [3.878931443809131352e+00, 1.669643226853416706e-01, -7.966037688832179775e+00, 1.999999986860410354e+04, 5.093236383218564811e-02, -1.860520726286646331e-02, 3.419286121868728330e-03],
    [3.944240477334767014e+00, 5.080492719524261958e+00, -7.884759241038421962e+00, 1.999999983343019630e+04, -9.584768355257994310e-02, -7.160437150334985311e-03, 3.419286121868728330e-03],
    [3.793663566611240157e+00, -9.084556487747148523e-01, -7.795303863762848451e+00, 1.999999981960374134e+04, -2.170484339864149279e-02, -4.256758101390815952e-02, 3.419286121868728330e-03],
    [3.835768395497900052e+00, -1.976910852770307869e+00, -7.884332564781925790e+00, 1.999999978540193842e+04, -4.857024104641133322e-02, -1.819386409384296521e-02, 3.419286121868728330e-03],
    [4.038737080429045001e+00, -3.219221683314476223e+00, -7.800055118714679381e+00, 1.999999985536181339e+04, -5.102259384637157502e-02, 4.113053952820507758e-02, 3.419286121868728330e-03],
    [3.997823379760361728e+00, -4.059484980271274246e+00, -7.795259103470438156e+00, 1.999999996417645889e+04, 4.336745810957862851e-02, -8.402339269272211808e-02, 3.419286121868728330e-03],
    [3.994134166406829944e+00, -3.568127305295409002e+00, -7.858114350973035123e+00, 1.999999996219778404e+04, -4.782615656678221999e-02, 5.685252089639994226e-03, 3.419286121868728330e-03]
],dtype=np.float64)

# filename = './transfer_trailer.txt'  # 替换为你的 .txt 文件名
# array = np.loadtxt(filename)
# shape = array.shape
# # 获取各个维度的大小
# dim = np.array([shape[0],shape[1]])
# dim1 = dim[0]
# dim2 =dim[1]
# print("IN PYTHON:","\n//Dimension 1 size:", dim1,"\n//Dimension 2 size:", dim2)

# array_ptr = array.ctypes.data_as(ctypes.POINTER(ctypes.c_double))

# # Call the Fortran subroutine
# libexample.go4(array_ptr, ctypes.byref(ctypes.c_int(dim1)), ctypes.byref(ctypes.c_int(dim2)))

array = np.asfortranarray(array)#按照fortran读取
array = array.flatten()#转为1维数组
shape = array.shape
dim = np.array(shape[0])
print("IN PYTHON:","\n//Dimension size:", dim)

# 获取每个粒子的位置
num_particles = 10  # 粒子数量
num_params_per_particle = 7  # 每个粒子的参数数量

# 定义一个函数来获取粒子的参数
def get_particle_params(array, particle_index, num_params):
    start_index = particle_index * num_params
    end_index = start_index + num_params
    return array[start_index:end_index]

# 打印每个粒子的参数
for i in range(num_particles):
    particle_params = get_particle_params(array, i, num_params_per_particle)
    tempz, tempx, tempy, tempvz, tempvx, tempvy, tempq = particle_params
    print(f"粒子 {i+1} 的参数:")
    print(f"tempz = {tempz}")
    print(f"tempx = {tempx}")
    print(f"tempy = {tempy}")
    print(f"tempvz = {tempvz}")
    print(f"tempvx = {tempvx}")
    print(f"tempvy = {tempvy}")
    print(f"tempq = {tempq}")
    print()

array_ptr = array.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
libexample.go4(array_ptr, ctypes.byref(ctypes.c_int(dim)))



